package com.hdb.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil extends Object {
	private static SessionFactory sessionFactory;
	static	
	{		
		try{			
			Configuration configuration=new Configuration().configure();
			ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();				
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);	  	
			}catch (Throwable ex){	            
			throw new ExceptionInInitializerError(ex);
		}	
	}
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static Session getSession() throws HibernateException {
		Session session = (Session) threadLocal.get();
		if (session == null) {
			session = sessionFactory.openSession();
			threadLocal.set(session);
		}
		return session;
	}

	public static void closeSession() throws HibernateException {
		Session session = (Session) threadLocal.get();
		if (session != null)
			session.close();
		threadLocal.set(null);
	}

	public static void shutdown() {
		getSessionFactory().close();
	}
}
